import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> ret = new ArrayList<Integer>();

        //用来存储words中的字符串信息
        Map<String,Integer> hash1 = new HashMap<String,Integer>();
        //存储words中字符串信息
        for(String str : words) hash1.put(str,hash1.getOrDefault(str,0)+1);

        int len = words[0].length();//每个字符串长度
        int n = words.length;//字符串个数
        //执行次数
        for(int i = 0; i < len ;i++){
            //滑动窗口
            Map<String,Integer> hash2 = new HashMap<String,Integer>();//存储s中的字符串信息
            for(int left = i,right = i,count = 0;right + len <= s.length();right+=len){
                //入窗口
                String in = s.substring(right,right+len);
                hash2.put(in,hash2.getOrDefault(in,0)+1);
                //count判断
                if(hash1.getOrDefault(in,0) >= hash2.get(in)) count++;

                //判断
                if(right-left+1 > len*n) {
                    String out = s.substring(left,left+len);
                    //count判断
                    if(hash1.getOrDefault(out,0) >= hash2.get(out)) count--;
                    //出窗口
                    hash2.put(out,hash2.get(out)-1);
                    left+=len;
                }
                if(count == n){
                    ret.add(left);
                }
            }
        }
        return ret;
    }
}
